package com.sls.game.parser.jieyitong;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.jieyitong.BalanceQueryResInfo;

/**
 * 
 * @description 余额查询消息返回解析
 * @author Wangxuegang
 * @date 2013-3-30
 */
public class ParseBalanceResMsg {

	private static Logger log = Logger.getLogger(ParseBalanceResMsg.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static BalanceQueryResInfo parseBalance(String xmlStr) {

		BalanceQueryResInfo info = new BalanceQueryResInfo();
		InputSource source = null;
		StringReader read = null;
		read = new StringReader(xmlStr);
		source = new InputSource(read);
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(source);
			Element root = document.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("userid".equals(node.getNodeName())) { // 第二层节点
					String userid = node.getTextContent();
					info.setUserid(userid);
				} else if ("money".equals(node.getNodeName())) {
					String money = node.getTextContent();
					info.setMoney(money);
				} else if ("resultno".equals(node.getNodeName())) {
					String resultno = node.getTextContent();
					info.setResultno(resultno);
				}
			}

		} catch (Exception e) {
			log.error(e);
		}

		return info;
	}

}
